﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using Aluetjen.EmailService.Domain.Infrastructure;
using Aluetjen.EmailService.Domain.Models.Endpoints;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Aluetjen.EmailService.Agent.Tests
{
    [TestClass]
    public class HostManagerTests
    {
        [TestMethod]
        public void OpenMissingHost()
        {
            var endpointRepository = MockRepository.GenerateStub<IRepository<Endpoint>>();
            var bindingFactory = MockRepository.GenerateStub<IBindingFactory>();
            bindingFactory.Stub( s => s.Create(null, null)).IgnoreArguments().Return(new BasicHttpBinding());

            const string address = "http://localhost:8080/EmailService";
            var endpoint = new Endpoint
                               {
                                   Address = address,
                                   Binding = "BasicHttpBinding",
                                   Id = 1,
                                   Version = 0,
                                   Enabled = true
                               };
            
            endpointRepository.Stub(r => r.All()).Return(new List<Endpoint> {endpoint}.AsQueryable());

            var manager = new HostManager(endpointRepository, bindingFactory, new HostContainer());

            for (int i = 0; i < 1000; i++)
            {
                endpoint.Version++;
                endpoint.Address = address + i;
                endpoint.Enabled = !endpoint.Enabled;

                manager.OpenMissingEndpoints();      
  
                Thread.Sleep(15000);
            }
        }
    }
}
